package applet.view;

import java.awt.BorderLayout;

import javax.swing.JPanel;

import applet.login.CMSLoginView;

public class CMSViewController extends JPanel
{
	private static final long serialVersionUID = -830455117406884706L;
	
	private CMSView view;
	public CMSView view() { return view; }
	
	private CMSView defaultView;
	public CMSView defaultView() { return defaultView; }
	public void setDefaultView(CMSView defaultView)
	{
		this.defaultView = defaultView;
		defaultView.setViewController(this);
	}
	
	public void deferToView(CMSView view)
	{
		if(this.view != view)
		{
			if(this.view != null)
			{
				this.view.makeInactive();
				remove(this.view.view());
			}
			
			this.view = view;
			if(view != null)
			{
				view.setViewController(this);
				view.makeActive();
				add(view.view(), BorderLayout.CENTER);
			}
			
			validate();
			repaint();
		}
	}
	
	public void deferToDefaultView() { deferToView(defaultView); }
	
	public CMSViewController()
	{
		this(new CMSLoginView());
	}
	
	public CMSViewController(CMSView defaultView)
	{
		setLayout(new BorderLayout());
		setDefaultView(defaultView);
		deferToDefaultView();
	}
}